import 'package:efood_multivendor/controller/restaurant_controller.dart';
import 'package:efood_multivendor/helper/responsive_helper.dart';
import 'package:efood_multivendor/helper/size_config.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/view/base/product_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class RestaurantView extends StatefulWidget {
  final ScrollController? scrollController;
  RestaurantView({this.scrollController});

  @override
  State<RestaurantView> createState() => _RestaurantViewState();
}

class _RestaurantViewState extends State<RestaurantView> {
  @override
  Widget build(BuildContext context) {
    Get.find<RestaurantController>().setOffset(1);
    widget.scrollController?.addListener(() {
      if (widget.scrollController!.position.pixels >
              widget.scrollController!.position.maxScrollExtent * 0.6 &&
          Get.find<RestaurantController>().restaurantList != null &&
          !Get.find<RestaurantController>().isLoading) {
        setState(() {});
        int pageSize =
            (Get.find<RestaurantController>().popularPageSize / 10).ceil();
        if (Get.find<RestaurantController>().offset < pageSize) {
          Get.find<RestaurantController>().showBottomLoader();
          Get.find<RestaurantController>()
              .setOffset(Get.find<RestaurantController>().offset + 1);

          Get.find<RestaurantController>().getRestaurantList(
              Get.find<RestaurantController>().offset.toString(), false);
        }
      }
    });
    return GetBuilder<RestaurantController>(builder: (restController) {
      return Stack(children: [
        ProductView(
          fromRestaurant: false,
          isRestaurant: true,
          products: [],
          restaurants: restController.restaurantList ?? [],
          padding: EdgeInsets.symmetric(
            horizontal: ResponsiveHelper.isDesktop(context)
                ? Dimensions.PADDING_SIZE_EXTRA_SMALL
                : Dimensions.PADDING_SIZE_SMALL,
            vertical: ResponsiveHelper.isDesktop(context)
                ? Dimensions.PADDING_SIZE_EXTRA_SMALL
                : 0,
          ),
        ),
        restController.isLoading
            ? Positioned(
                bottom: 10.h,
                left: 45.w,
                child: Center(
                    child: Padding(
                  padding:
                      EdgeInsets.only(bottom: Dimensions.PADDING_SIZE_SMALL),
                  child: CircularProgressIndicator.adaptive(
                    valueColor:
                        AlwaysStoppedAnimation<Color>(Color(0xffff2222)),
                    backgroundColor: Color(0xffff8022),
                  ),
                )),
              )
            : SizedBox(),
      ]);
    });
  }
}
